<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%product}}".
 *
 * @property int $id
 * @property string $ttid 商品ID
 * @property string $name 商品名称
 * @property string $price 原价
 * @property string $newprice 新价格
 * @property string $image 商品图片
 * @property int $coupon_id  优惠券id
 * @property string $description 拼团和参团进行中页面文案
 * @property string $remark 商品描述
 * @property string $notes 注
 * @property string $share_title 微信分享标题
 * @property string $share_img 微信分享图片
 * @property string $share_txt 微信分享文案
 * @property string $share_url 微信分享连接
 * @property int $activity_id 活动ID
 *
 */
class Product extends \yii\db\ActiveRecord
{
    public $pic,$pic1;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%product}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'price', 'newprice', 'coupon_id'], 'required'],
            [['price', 'newprice'], 'number'],
            [['coupon_id','activity_id'], 'integer'],
            [['remark','notes'], 'string'],
            [['ttid','name','image','description','share_title','share_img','share_txt','share_url'], 'string', 'max' => 255],
            [['pic'],'file','skipOnEmpty'=>false,'extensions'=>'png,jpg,jpeg,gif','when'=>function($model){
                    return !empty($model->pic);
            },'whenClient'=>"function(attribute,value){
                return false;
            }"],
            [['pic1'],'file','skipOnEmpty'=>false,'extensions'=>'png,jpg,jpeg,gif','when'=>function($model){
                return !empty($model->pic1);
            },'whenClient'=>"function(attribute,value){
                return false;
            }"]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'ttid' => '商品ID',
            'name' => '商品名称',
            'price' => '原价',
            'newprice' => '新价格',
            'image' => '商品图片',
            'coupon_id' => ' 优惠券id',
            'description' => '拼团和参团进行中页面文案',
            'remark' => '商品描述',
            'notes' => '注',
            'pic'=>'商品图片',
            'pic1'=>'微信分享图片',
            'share_title' => '微信分享标题',
            'share_img' => '微信分享图片',
            'share_txt' => '微信分享文案',
            'share_url' => '微信分享连接',
            'activity_id' => '活动ID',
        ];
    }

    public function upload(){
        if($this->validate('pic')){
//            $imgname='product_'.date('YmdHis').rand(1000,9999);
//
//            $this->pic->saveAs(Yii::$app->basePath.'/web/uploads/product/'.$imgname.'.'.$this->pic->extension);
//            return '/admin/uploads/product/'.$imgname.'.'.$this->pic->extension;
            $savePath = 'wumaituangou/'.date('Ymd').'/product/';
            $filename = uniqid().'.'.$this->pic->extension;

            $stream = fopen($this->pic->tempName, 'r+');
            if (!Yii::$app->awss3Fs->putStream($savePath . $filename, $stream)) {
                throw new yii\web\ServerErrorHttpException('File upload failed.');
            }
            @unlink($this->pic->tempName);
//            var_dump(getenv('OSS_FILE_ROOT') . $savePath . $filename);exit;
            return getenv('OSS_FILE_ROOT') . $savePath . $filename;
        }else{
            return false;
        }

    }
    public function upload1(){
        if($this->validate('pic1')){
//            $imgname='share_'.date('YmdHis').rand(1000,9999);
//            $this->pic1->saveAs(Yii::$app->basePath.'/web/uploads/share/'.$imgname.'.'.$this->pic1->extension);
//            return '/admin/uploads/share/'.$imgname.'.'.$this->pic1->extension;
            $savePath = 'wumaituangou/'.date('Ymd').'/share/';
            $filename = uniqid().'.'.$this->pic1->extension;

            $stream = fopen($this->pic1->tempName, 'r+');
            if (!Yii::$app->awss3Fs->putStream($savePath . $filename, $stream)) {
                throw new yii\web\ServerErrorHttpException('File upload failed.');
            }
            @unlink($this->pic1->tempName);
//            var_dump(getenv('OSS_FILE_ROOT') . $savePath . $filename);exit;
            return getenv('OSS_FILE_ROOT') . $savePath . $filename;
        }else{
            return false;
        }

    }
}
